﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Data;
using System.Windows.Documents;
using System.Windows.Input;
using System.Windows.Media;
using System.Windows.Media.Imaging;
using System.Windows.Navigation;
using System.Windows.Shapes;

namespace OpticalPowerMeter.UserControls
{
    /// <summary>
    /// 线性点设置控件按钮点击时产生事件，事件数据存放哪个按钮被点击了
    /// </summary>
    public class ButtonClickEventArgs : EventArgs
    {
        public string ButtonName { set; get; }
    }
    /// <summary>
    /// LinearControl.xaml 的交互逻辑
    /// </summary>
    public partial class LinearControl : UserControl
    {
        /// <summary>
        /// "+","-"按钮点击时，产生一个事件
        /// </summary>
        public event EventHandler<ButtonClickEventArgs> LinearClick = null;

        public LinearControl()
        {
            InitializeComponent();
        }

        #region 尺寸属性
        //字号
        static DependencyProperty MyFontSizeProperty = DependencyProperty.Register(
            "MyFontSize",
            typeof(double),
            typeof(LinearControl)
            );

        //"校准点1功率"标签边距
        static DependencyProperty Label1MarginProperty = DependencyProperty.Register(
            "Label1Margin",
            typeof(Thickness),
            typeof(LinearControl)
            );

        //输入框、按钮等的高度
        static DependencyProperty BoxHeightProperty = DependencyProperty.Register(
            "BoxHeight",
            typeof(double),
            typeof(LinearControl)
            );

        //输入框宽度
        static DependencyProperty BoxWidthProperty = DependencyProperty.Register(
            "BoxWidth",
            typeof(double),
            typeof(LinearControl)
            );

        //第一个校准点输入框的边距
        static DependencyProperty P1MarginProperty = DependencyProperty.Register(
            "P1Margin",
            typeof(Thickness),
            typeof(LinearControl)
            );

        //"校准点2功率"标签边距
        static DependencyProperty Label2MarginProperty = DependencyProperty.Register(
            "Label2Margin",
            typeof(Thickness),
            typeof(LinearControl)
            );

        //第二个校准点输入框的边距
        static DependencyProperty P2MarginProperty = DependencyProperty.Register(
            "P2Margin",
            typeof(Thickness),
            typeof(LinearControl)
            );

        //添加按钮的边距
        static DependencyProperty AddButtonMarginProperty = DependencyProperty.Register(
            "AddButtonMargin",
            typeof(Thickness),
            typeof(LinearControl)
            );

        //删除按钮的边距
        static DependencyProperty DeleteButtonMarginProperty = DependencyProperty.Register(
            "DeleteButtonMargin",
            typeof(Thickness),
            typeof(LinearControl)
            );
        #endregion

        #region 值属性
        //P1的值
        static DependencyProperty P1Property = DependencyProperty.Register(
            "P1",
            typeof(string),
            typeof(LinearControl)
            );

        //P2的值
        static DependencyProperty P2Property = DependencyProperty.Register(
            "P2",
            typeof(string),
            typeof(LinearControl)
            );

        #endregion

        /// <summary>
        /// 第一个校准点的值
        /// </summary>
        public string P1 {
            get {
                return (string)GetValue(P1Property);
            }
            set
            {
                SetValue(P1Property,value);
            }
        }

        /// <summary>
        /// 第二个校准点的值
        /// </summary>
        public string P2
        {
            get { return (string)GetValue(P2Property); }
            set { SetValue(P2Property, value); }
        }

        //添加按钮点击
        private void AddButton_Click(object sender,RoutedEventArgs args) {
            ButtonClickEventArgs buttonClickEventArgs = new ButtonClickEventArgs();
            buttonClickEventArgs.ButtonName = "AddButton";
            LinearClick?.Invoke(this,buttonClickEventArgs);
        }

        //删除按钮点击
        private void DeleteButton_Click(object sender,RoutedEventArgs args)
        {
            ButtonClickEventArgs buttonClickEventArgs = new ButtonClickEventArgs();
            buttonClickEventArgs.ButtonName = "DeleteButton";
            LinearClick?.Invoke(this, buttonClickEventArgs);
        }

        //尺寸变化时，修改控件位置及尺寸
        private void Size_Changed(object sender,SizeChangedEventArgs args) {
            double spliteWidth = 5.0; //控件与控件间隔5
            double width = ActualWidth; //获取控件的宽度
            double height = ActualHeight; //获取控件的高度
            double fontsizeFromHeight = Math.Round((height - 10) / 1.27,2); //根据高度计算字号
            double fontsizeFromWidth = Math.Round((width - spliteWidth * 7 - 20) / 21.34,2); //根据宽度计算的字号
            double fontsize = fontsizeFromHeight > fontsizeFromWidth ? fontsizeFromWidth : fontsizeFromHeight; //取两种计算方式中小的作为字号
            SetValue(MyFontSizeProperty, fontsize); //设置字号
            double label1Left = spliteWidth;
            SetValue(Label1MarginProperty,new Thickness(label1Left, 0,0,0)); //设置"校准点1功率"标签边距
            double boxHeight = fontsize * 1.27; //输入框高度
            SetValue(BoxHeightProperty,boxHeight);
            SetValue(BoxWidthProperty, boxHeight * 3);
            double textbox1Left = label1Left + spliteWidth + fontsize * 5.59 + 10;
            SetValue(P1MarginProperty, new Thickness(textbox1Left,0,0,0)); //P1边距
            double label2Left = textbox1Left + spliteWidth + boxHeight * 3;
            SetValue(Label2MarginProperty, new Thickness(label2Left,0,0,0)); // 设置"校准点2功率"标签边距
            double textbox2Left= label2Left + spliteWidth + fontsize * 5.59 + 10;
            SetValue(P2MarginProperty,new Thickness(textbox2Left,0,0,0)); //P2边距
            double addButtonLeft= textbox2Left+spliteWidth + boxHeight * 3;
            SetValue(AddButtonMarginProperty,new Thickness(addButtonLeft,0,0,0)); //"+"边距
            double deleButtonLeft = addButtonLeft + spliteWidth / 2 + boxHeight;
            SetValue(DeleteButtonMarginProperty,new Thickness(deleButtonLeft,0,0,0)); //"-"边距
        }
    }
}
